package com.example.geguo.imclient;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import com.itheima.im.socket.bean.QQMessage;
import com.itheima.im.socket.bean.QQMessageType;
import com.thoughtworks.xstream.XStream;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
	@Test
	public void useAppContext() throws Exception {
		// Context of the app under test.
		Context appContext = InstrumentationRegistry.getTargetContext();

		assertEquals("com.example.geguo.imclient", appContext.getPackageName());
	}
	@Test
	public void testToXMLObject() {

		QQMessage msg = new QQMessage();
		msg.type = QQMessageType.MSG_TYPE_BUDDYLIST;
		msg.content = "在不在";
		// 创建核心类
		XStream x = new XStream();
		x.alias("QQMessage", QQMessage.class);
		// 调用toxml
		String xml = x.toXML(msg);
		System.out.println(xml);
		QQMessage msg2 = (QQMessage) x.fromXML(xml);
		System.out.println(msg2.content);
		//
		// 06-11 21:47:16.526: I/System.out(1968): <QQMessage>
		// 06-11 21:47:16.526: I/System.out(1968): <content>在不在</content>
		// 06-11 21:47:16.526: I/System.out(1968): <type>buddylist</type>
		// 06-11 21:47:16.526: I/System.out(1968): <sendTime>06-11
		// 21:47:16</sendTime>
		// 06-11 21:47:16.526: I/System.out(1968): <fromNick></fromNick>
		// 06-11 21:47:16.526: I/System.out(1968): <from>0</from>
		// 06-11 21:47:16.526: I/System.out(1968): <to>0</to>
		// 06-11 21:47:16.526: I/System.out(1968): <fromAvatar>1</fromAvatar>
		// 06-11 21:47:16.526: I/System.out(1968): </QQMessage>

	}

	@Test
	public void testFromXMLString() {

		QQMessage msg = new QQMessage();
		msg.type = QQMessageType.MSG_TYPE_BUDDYLIST;
		msg.content = "在不在";
		// System.out.println(msg.toXml());
		// QQMessage msg2=(QQMessage) msg.fromXml(msg.toXml());
		// System.out.println(msg2.content);
		String json = msg.toJson();
		System.out.println(json);

		QQMessage msg2 = (QQMessage) msg.fromJson(json);
		System.out.println(msg2);
	}
}
